package com.gree.flowable.app.controller;

import com.gree.flowable.app.pojo.form.FormInfo;
import com.gree.flowable.app.pojo.to.FlwFormNodeSetTO;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFlwFormNodeSetService;
import com.gree.flowable.app.services.IFormInfoService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/4/15 9:06
 * @description：
 * @modified By：
 * @version: 1.0
 */

@RestController
@RequestMapping("/rest/formNode")
@Api(tags = "表单与流程节点关系")
public class IFlwFormNodeSetApi {
    @Autowired
    private IFlwFormNodeSetService iFlwFormNodeSetService;

    @Autowired
    private IFormInfoService iFormInfoService;

    @PostMapping("/addFormNode")
    @ApiOperation("添加表单与流程节点的关系")
    public ReturnVO addFormNode(@RequestBody FlwFormNodeSetTO flwFormNodeSetTO){
        String formId = flwFormNodeSetTO.getFormId();
        String processKey = flwFormNodeSetTO.getProcessKey();
        FormInfo formInfo = iFormInfoService.queryFormById(formId);
        if (formInfo.getFormSync()== 0){
            return new ReturnVO(ReturnCode.FAIL, "该表单还未同步到数据库！");
        }

        ReturnVO result = iFlwFormNodeSetService.addFormNode(processKey, formId);
        return result;
    }

    @DeleteMapping("/process/{processKey}/{formId}")
    @ApiOperation("解除表单与流程关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processKey",value = "流程关键字",dataType = "String"),
            @ApiImplicitParam(name = "formId",value = "表单ID",dataType = "String")
    })
    public ReturnVO deleteFormNode(@PathVariable String processKey, @PathVariable String formId){
        ReturnVO result = iFlwFormNodeSetService.deleteFormNode(processKey,formId);
        return result;
    }

    @PostMapping("/updateFormNode")
    @ApiOperation("更新表单与流程节点的关系")
    public ReturnVO updateFormNode(@RequestBody FlwFormNodeSetTO flwFormNodeSetTO){
        String formId = flwFormNodeSetTO.getFormId();
        String processKey = flwFormNodeSetTO.getProcessKey();
        ReturnVO result = iFlwFormNodeSetService.updateFormNode(processKey, formId);
        return result;
    }

    @PostMapping("/getFieldState")
    @ApiOperation("获取表单项状态")
    public ReturnVO queryByProcessAndNode(@RequestBody Map<String,Object> param){
        String processKey = param.get("processKey") == null ? "" : param.get("processKey").toString();
        String elementId = param.get("elementId") == null ? "" : param.get("elementId").toString();
        Integer  formType= (Integer) param.get("formType");
        ReturnVO result = iFlwFormNodeSetService.queryByProcessAndNode(processKey, elementId,formType);
        return result;
    }

    @PostMapping("/updateNodeState")
    @ApiOperation("更新流程某个节点的表单项状态")
    public ReturnVO updateNodeState(@RequestBody Map<String,Object> param){
        String processKey = param.get("processKey") == null ? "" : param.get("processKey").toString();
        String elementId = param.get("elementId") == null ? "" : param.get("elementId").toString();
        String formId = param.get("formId") == null ? "" : param.get("formId").toString();
        List<Map<String,Object>> fieldList = (List<Map<String, Object>>) param.get("fieldList");
        ReturnVO result = iFlwFormNodeSetService.updateFieldNodeState(processKey, elementId,formId, fieldList);
        return result;
    }

    @GetMapping("/form/{processKey}/{formType}")
    @ApiOperation("根据流程关键字与表单类型查询info")
    public ReturnVO getInfoByKeyAndType(@PathVariable String processKey, @PathVariable Integer formType){
        return iFlwFormNodeSetService.getInfoByProcessKeyAndType(processKey, formType);
    }
}
